function login() {
	//checks if the fields are filled
	if(user.value.length === 0) {
		user.focus();
		return;
	}else if(pw.value.length === 0) {
		pw.focus();
		return;
	}
	//checks if there are any whitespace characters in the fields
	if( (user.value.search(/[\s]+/) >= 0) || (pw.value.search(/[\s]+/) >= 0) ) {
		//complain about bad user input here
		document.getElementById("error").innerHTML= "Du matade in fel användarnamn eller lösenord!";
		//return as we do not want to continue
		return;
	}
	//pattern-matching so we can remove bad input
	if(user.value.search(/[\"\'\>\<\}\{]+/) >= 0) {
		//complain about bad user input here
		document.getElementById("error").innerHTML= "Du matade in fel användarnamn eller lösenord!";
		//return as we do not want to continue
		return;
	}
	//no error is found so we can submit the form containing the login information
	document.getElementById("loginForm").submit();
	
}

function fieldClick(e) {
	if(e.keyCode !== 13)
		return;
	
	if(document.activeElement.className === "inputLogin") {
		if(document.activeElement.id === "user"){
			if(user.value.length > 0) {
				inputElements[1].focus();
			}
		}else if(document.activeElement.id === "password"){
			if(pw.value.length > 0) {
				inputElements[1].blur();
				login();
			}
		}
	}else {
		login();
	}
}

//get the login-button element
var loginElement = document.getElementById("login");
//set a listener to the button
loginElement.addEventListener("click", login);

//contains the information we try to log in with
var user = document.getElementById("user");
var pw = document.getElementById("password");
	
//get the input-fields in the form
var inputElements = document.getElementsByClassName("inputLogin");
//set eventlisteners to the inputfields
document.addEventListener("keyup", fieldClick);